package com.neuedu.controller.backend;

import com.github.pagehelper.PageInfo;
import com.neuedu.common.Const;
import com.neuedu.common.JSONData;
import com.neuedu.pojo.Product;
import com.neuedu.pojo.UserInfo;
import com.neuedu.service.OrderService;
import com.neuedu.vo.OrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/manager/order/")
@Api(value = "管理员订单相关接口",description = "管理员订单相关接口")
public class ManagerOrderController {
    @Autowired
    private OrderService orderService;
    @ApiOperation(value = "订单列表",notes = "订单列表",httpMethod = "POST")
    @RequestMapping(value = "orderList",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<PageInfo<OrderVo>> orderList(HttpSession session,
                                                 @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                                 @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        // 1、判断用户是否登录
        UserInfo userInfo = (UserInfo) session.getAttribute(Const.CURRENT_USERINFO);
        if(userInfo == null){
            return JSONData.buildError("用户未登录，请先登录");
        }
        //2、 判断其是否是管理员
        if(userInfo.getRole()==0){
            JSONData<PageInfo<OrderVo>> data = orderService.orderList(null, pageNum, pageSize);
            return data;
        }else{
            return JSONData.buildError("此用户无权限，请联系管理员！");
        }
    }
    @ApiOperation(value = "搜索订单",notes = "搜索订单",httpMethod = "POST")
    @RequestMapping(value = "orderSearch",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<OrderVo> orderSearch(HttpSession session,Long orderNo){
        // 1、判断用户是否登录
        UserInfo userInfo = (UserInfo) session.getAttribute(Const.CURRENT_USERINFO);
        if(userInfo == null){
            return JSONData.buildError("用户未登录，请先登录");
        }
        //2、 判断其是否是管理员
        if(userInfo.getRole()==0){
            JSONData<OrderVo> data = orderService.orderDetail(null, orderNo);
            return data;
        }else{
            return JSONData.buildError("此用户无权限，请联系管理员！");
        }
    }
    @ApiOperation(value = "发送订单",notes = "发送订单",httpMethod = "POST")
    @RequestMapping(value = "sendOrder",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<String> sendOrder(HttpSession session,Long orderNo){
        // 1、判断用户是否登录
        UserInfo userInfo = (UserInfo) session.getAttribute(Const.CURRENT_USERINFO);
        if(userInfo == null){
            return JSONData.buildError("用户未登录，请先登录");
        }
        //2、 判断其是否是管理员
        if(userInfo.getRole()==0){
            JSONData<String> data = orderService.sendOrder(orderNo);
            return data;
        }else{
            return JSONData.buildError("此用户无权限，请联系管理员！");
        }
    }
}
